/**
 * 
 */
package com.metagurukul.metaspace.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

/**
 * @author himanshu
 * 
 */
public class ChangePictureForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private FormFile selectFile;

	public void setSelectFile(FormFile selectFile) {
		this.selectFile = selectFile;

	}

	public FormFile getSelectFile() {
		return selectFile;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.setSelectFile(null);
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		ActionErrors errors = new ActionErrors();
		FormFile formFile = null;
		if (formFile.getFileSize() == 0) {
			errors.add("selectFile", new ActionMessage("error.file.required"));
		} else if(!(formFile.getFileName().length()<1)){
			errors.add("selectFile", new ActionMessage("error.file.required"));
		} else if (!formFile.getContentType().equals("image/jpeg")
				&& !formFile.getContentType().equals("image/png")
				&& !formFile.getContentType().equals("image/jpg")
				&& !formFile.getContentType().equals("image/gif")) {
			errors.add("selectFile", new ActionMessage("error.file.type"));
		}
		/**
		 * If the file size is greater than 300KB.
		 */
		else if (formFile.getFileSize() > 307200) {
			errors.add("selectFile", new ActionMessage("error.file.size"));
		}
		HttpSession session = request.getSession(true);
		String fileName = session.getAttribute("id").toString()+".jpg";
		setFileName(fileName);
		return mapping.findForward("success");
	}

	private void setFileName(String fileName) {
		selectFile.setFileName(fileName);
	}

	public String toString() {
		return "file=" + String.valueOf(this.getSelectFile());
	}
}